#   /**
#   * Copyright (c) 2022 Beijing Jiaotong University
#   * PhotLab is licensed under [Open Source License].
#   * You can use this software according to the terms and conditions of the [Open Source License].
#   * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
#   *
#   * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#   * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#   * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#   *
#   * See the [Open Source License] for more details.
#   */
#   /**
#   * Author: Zheng Wang
#   * Created: Sep. 28, 2022
#   * Supported by: National Key Research and Development Program of China
#   */
import json
import toml


def parse_json(file_path):
    """
    parse json file into python data type
    :param file_path:a json filepath
    :return:
    """
    if file_path is None or len(file_path) <= 0:
        return None
    else:
        with open(file_path, "r", encoding="utf-8") as f:
            data = json.load(f)
        return data


def export_data_from_toml(toml_filepath):
    """
        Load data from given file path
    :param toml_filepath:
    :return: dict
            {
                "system_info":[{},...,{}],
                "vertex":[{},...,{}],
                "edge":[{},...,{}]
            }
    """
    system_data = toml.load(toml_filepath)
    return system_data


def parse_edge_list_from_phtg_dict(phtg_dict: dict) -> list:
    edge_dict_list = []
    edge_data_list = phtg_dict["Graph"]["links"]
    # print(f"edge_data_list为{edge_data_list}")
    for edge_data in edge_data_list:
        edge_dict = {}
        edge_dict["sourceVertexId"] = str(edge_data["srcNodeId"])
        edge_dict["sourcePortId"] = str(edge_data["srcPortId"])
        edge_dict["destinationVertexId"] = str(edge_data["dstNodeId"])
        edge_dict["destinationPortId"] = str(edge_data["dstPortId"])
        edge_dict["signalType"] = ""
        edge_dict_list.append(edge_dict)
    return edge_dict_list


def parse_vertex_dict_list_from_phtg_dict(phtg_dict: dict) -> list:
    vertex_data_list = phtg_dict["Graph"]["components"]
    # print(f"vertex_data_list为{vertex_data_list}")
    vertex_dict_list = []
    for vertex_data in vertex_data_list:
        vertex_dict = {}
        vertex_dict["componentId"] = str(vertex_data["componentId"])
        vertex_dict["ptName"] = vertex_data["name"]
        vertex_dict["calculationMethod"] = vertex_data["calculationMethod"]
        vertex_dict["parallelComputingMethod"] = vertex_data["parallelComputingMethod"]
        vertex_dict["isAnalyzer"] = vertex_data["isAnalyzer"]
        vertex_dict["paramList"] = []
        for parameter_dict in vertex_data["parameters"]:
            if len(parameter_dict) > 0:
                format_parameter_dict = {}
                format_parameter_dict["pyPhotName"] = parameter_dict["name"]
                format_parameter_dict["dataType"] = parameter_dict["dataType"]
                format_parameter_dict["value"] = parameter_dict["value"]
                format_parameter_dict["unit"] = parameter_dict["unit"]
                format_parameter_dict["photUnit"] = parameter_dict["photUnit"]
                vertex_dict["paramList"].append(format_parameter_dict)
        vertex_dict_list.append(vertex_dict)
    return vertex_dict_list


def parse_global_parameter_from_phtg(phtg_dict: dict) -> list:
    global_parameter_dict_list = []
    global_parameter_list = phtg_dict["Graph"]["graphParameters"]["systemGlobalSimulationParameter"]
    # print(f"global_parameter_list为{global_parameter_list}")
    for elem in global_parameter_list:
        if len(elem) == 0 or elem is None:
            return []
    for global_parameter_data in global_parameter_list:
        global_parameter_dict = {}
        global_parameter_dict["pyPhotName"] = global_parameter_data["name"]
        global_parameter_dict["dataType"] = global_parameter_data["dataType"]
        global_parameter_dict["value"] = global_parameter_data["value"]
        global_parameter_dict["unit"] = global_parameter_data["unit"]
        global_parameter_dict["photUnit"] = global_parameter_data["photUnit"]
        global_parameter_dict_list.append(global_parameter_dict)
    return global_parameter_dict_list


def export_data_from_phtg(phtg_filepath):
    """
        从 *.phtg中直接解析信息
    :param phtg_filepath:
    :return:
    """
    system_data = toml.load(phtg_filepath)
    vertex_list = parse_vertex_dict_list_from_phtg_dict(phtg_dict=system_data)
    edge_list = parse_edge_list_from_phtg_dict(phtg_dict=system_data)
    global_parameter_list = parse_global_parameter_from_phtg(phtg_dict=system_data)
    return vertex_list, edge_list,global_parameter_list


if __name__ == '__main__':
    PHTG_PATH = r"../GDUT_20240213_without_global_parameter.phtg"
    # vertex_data = export_data_from_toml(PHTG_PATH)
    # formatted_data = json.dumps(vertex_data, indent=4, sort_keys=True)
    # print(formatted_data)
    # print(vertex_data)
    vertex_data, edge_data,global_parameter_data = export_data_from_phtg(phtg_filepath=PHTG_PATH)
    print(vertex_data)
    print(edge_data)
    print(global_parameter_data)
